package com.jack.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.transaction.Transactional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.jack.dto.CartDto;
import com.jack.dto.OrderDto;
import com.jack.enums.OrderStatusEnum;
import com.jack.enums.PayStatusEnum;
import com.jack.enums.ResultEnum;
import com.jack.exception.SellException;
import com.jack.model.OrderDetail;
import com.jack.model.OrderMaster;
import com.jack.model.ProductInfo;
import com.jack.repository.OrderDetailRespository;
import com.jack.repository.OrderMasterRepository;
import com.jack.service.OrderService;
import com.jack.service.ProductInfoService;
import com.jack.utils.KeyGenerator;


@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderDetailRespository orderDetailRepository;


	@Autowired
	private OrderMasterRepository orderMasterRepository;
	
	@Autowired
	private ProductInfoService productService;	
	@Override
	@Transactional
	public OrderDto create(OrderDto orderDto) {
		String orderId = KeyGenerator.uniqueKey();
		BigDecimal orderAmount = new BigDecimal(0);
//		List<CartDto> cartDtoList = new ArrayList<>();
		//1. query product 
		List<OrderDetail> orderDetailList = orderDto.getOrderDetailList();
		for (OrderDetail orderDetail : orderDetailList) {
			ProductInfo productInfo = productService.findOne(orderDetail.getProductId());
			if(productInfo == null) {
				throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
			}
			//2. caculate amount
			 orderAmount = productInfo.getProductPrice()
					.multiply(new BigDecimal(orderDetail.getProductQuantity())) 
					.add(orderAmount);
			 BeanUtils.copyProperties(productInfo, orderDetail);
			 orderDetail.setOrderId(orderId);
			 orderDetail.setDetailId(KeyGenerator.uniqueKey());
			
			 orderDetailRepository.save(orderDetail);
			 
//			 CartDto cartDto = new CartDto(productInfo.getProductId(), productInfo.getProducStock());
//			 cartDtoList.add(cartDto);
		}
	
		
		//3. write to ddb
		OrderMaster orderMaster = new OrderMaster();
		BeanUtils.copyProperties(orderDto, orderMaster);
		orderMaster.setOrderId(orderId);
		orderMaster.setOrderAmount(orderAmount);
		orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
		orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
		orderMasterRepository.save(orderMaster);
		//4. minus stock
		List<CartDto> cartDtoList = new ArrayList<>();
		 orderDto.getOrderDetailList().stream().map(e -> new CartDto(e.getProductId(), e.getProductQuantity()))
		 .collect(Collectors.toList());
		productService.decreaseStock(cartDtoList);
		
		return orderDto;
	}

	@Override
	public OrderDto cancel(OrderDto orderDto) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public OrderDto finish(OrderDto orderDto) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public OrderDto paid(OrderDto orderDto) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public OrderDto findOne(String orderId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page<OrderDto> findList(String buyerOpenid, Pageable pageable) {
		// TODO Auto-generated method stub
		return null;
	}

}
